package com.ruoyi.form.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.FormInfo;
import com.ruoyi.db.domain.FormInfoProfit;
import com.ruoyi.db.mapper.FormInfoHisMapper;
import com.ruoyi.db.mapper.FormInfoMapper;
import com.ruoyi.db.mapper.FormInfoProfitMapper;
import com.ruoyi.db.mapper.MemberExtInfoMapper;
import com.ruoyi.db.model.param.FormInfoProfitParam;
import com.ruoyi.form.service.IFormInfoProfitService;
import com.ruoyi.market.utils.SysNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * profitService业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-23
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class FormInfoProfitServiceImpl implements IFormInfoProfitService 
{
    @Autowired
    private FormInfoProfitMapper formInfoProfitMapper;
    @Autowired
    private FormInfoMapper formInfoMapper;
    @Autowired
    private MemberExtInfoMapper memberExtInfoMapper;

    /**
     * 查询profit
     * 
     * @param id profit主键
     * @return profit
     */
    @Override
    public FormInfoProfit selectFormInfoProfitById(Long id)
    {
        return formInfoProfitMapper.selectFormInfoProfitById(id);
    }

    /**
     * 查询profit列表
     * 
     * @param formInfoProfit profit
     * @return profit
     */
    @Override
    public List<FormInfoProfit> selectFormInfoProfitList(FormInfoProfit formInfoProfit)
    {
        return formInfoProfitMapper.selectFormInfoProfitList(formInfoProfit);
    }

    /**
     * 查询profit列表
     *
     * @param formInfoProfit profit
     * @return profit
     */
    @Override
    public List<Map<String, Object>> selectFormInfoProfitListMap(FormInfoProfitParam formInfoProfit)
    {
        return formInfoProfitMapper.selectFormInfoProfitListMap(formInfoProfit);
    }

    /**
     * 新增profit
     * 
     * @param formInfoProfit profit
     * @return 结果
     */
    @Override
    public int insertFormInfoProfit(FormInfoProfit formInfoProfit)
    {

        FormInfo formInfo = formInfoMapper.selectFormInfoById(String.valueOf(formInfoProfit.getInfoId()));
        formInfoProfit.setInfoName(formInfo.getChannelName());

        formInfoProfit.setCreateTime(DateUtils.getNowDate());
        return formInfoProfitMapper.insertFormInfoProfit(formInfoProfit);
    }

    /**
     * 修改profit
     * 
     * @param formInfoProfit profit
     * @return 结果
     */
    @Override
    public int updateFormInfoProfit(FormInfoProfit formInfoProfit)
    {
        FormInfoProfit infoProfit = this.selectFormInfoProfitById(formInfoProfit.getId());
        // 查询推送成功数量
        formInfoProfit.setAvgAmount(NumberUtil.div(formInfoProfit.getProfitAmount(), infoProfit.getPushSuccessNumber(),2));

        formInfoProfit.setUpdateTime(DateUtils.getNowDate());
        return formInfoProfitMapper.updateFormInfoProfit(formInfoProfit);
    }

    /**
     * 批量删除profit
     * 
     * @param ids 需要删除的profit主键
     * @return 结果
     */
    @Override
    public int deleteFormInfoProfitByIds(String ids)
    {
        return formInfoProfitMapper.deleteFormInfoProfitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除profit信息
     * 
     * @param id profit主键
     * @return 结果
     */
    @Override
    public int deleteFormInfoProfitById(Long id)
    {
        return formInfoProfitMapper.deleteFormInfoProfitById(id);
    }

    @Override
    public Map<String, Object> selectFormInfoProfitTotalMap(FormInfoProfitParam formInfoProfit) {
        return formInfoProfitMapper.selectFormInfoProfitTotalMap(formInfoProfit);
    }

    @Override
    public List<Map<String, Object>> selectProfitTypeMapList() {
        return formInfoProfitMapper.selectProfitTypeMapList();
    }

    @Override
    public List<Map<String, Object>> getFormProfitInfo(Integer type) {

        LocalDate localDate = LocalDate.now().minusDays(type);
        String dateInfo = localDate.toString();

        Long totalNumber = memberExtInfoMapper.countAuthNumber(dateInfo);
        List<Map<String, Object>> resultList = formInfoProfitMapper.getProfirInfoList(dateInfo);

        final BigDecimal[] totamAmount = {BigDecimal.ZERO};
        resultList.stream().forEach(map -> {
            Integer number=  (Integer) map.get("number");
            BigDecimal amount=  (BigDecimal) map.get("amount");
            map.put("rate", SysNumberUtil.getPointNumber(new BigDecimal(number), new BigDecimal(totalNumber), 4 ));

            // 查询当天的收益
            totamAmount[0] = totamAmount[0].add(amount);
        });

        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "全部");
        totalMap.put("number", totalNumber);
        totalMap.put("rate", "100");
        totalMap.put("amount", totamAmount[0]);

        resultList.add(0, totalMap);

        return resultList;
    }

}
